//
//  PYTabBarController.m
//  xuexi
//
//  Created by Jim on 2018/4/7.
//  Copyright © 2018年 Jim. All rights reserved.
//

#import "PYTabBarController.h"

#import "PYLiveLessonViewController.h"

#import "PYCommunityViewController.h"

#import "PYMessageViewController.h"

#import "PYMineViewController.h"

#import "PYLoginNavigationController.h"

#import "PYLoginViewController.h"

#import "PYSearchLiveLessonViewController.h"

@interface PYTabBarController () <UITabBarControllerDelegate>

@property (nonatomic, strong) PYLiveLessonViewController *liveLessonVC;

@property (nonatomic, strong) PYCommunityViewController *communityVC;

@property (nonatomic, strong) PYMessageViewController *messageVC;

@property (nonatomic, strong) PYMineViewController *mineVC;

@end

@implementation PYTabBarController

- (PYLiveLessonViewController *)liveLessonVC
{
    if (!_liveLessonVC) {
        
        _liveLessonVC = [[PYLiveLessonViewController alloc] init];
        
        _liveLessonVC.tabBarItem.image = [UIImage imageNamed:@"bottom_zhiboke_moren_icon"];
        
        _liveLessonVC.tabBarItem.selectedImage = [UIImage imageNamed:@"bottom_zhiboke_xzh_icon"];
    
        _liveLessonVC.tabBarItem.title = @"直播课";
    }
    
    return _liveLessonVC;
}

- (PYCommunityViewController *)communityVC
{
    if (!_communityVC) {
        
        _communityVC = [[PYCommunityViewController alloc] init];
        
        _communityVC.tabBarItem.image = [UIImage imageNamed:@"bottom_shequ_moren_icon"];
        
        _communityVC.tabBarItem.selectedImage = [UIImage imageNamed:@"bottom_shequ_xzh_icon"];
    
        _communityVC.tabBarItem.title = @"社区";
    }
    
    return _communityVC;
}

- (PYMessageViewController *)messageVC
{
    if (!_messageVC) {
        
        _messageVC = [[PYMessageViewController alloc] init];
        
        _messageVC.tabBarItem.image = [UIImage imageNamed:@"bottom_xiaoxi_moren_icon"];
        
        _messageVC.tabBarItem.selectedImage = [UIImage imageNamed:@"bottom_xiaoxi_xzh_icon"];
   
        _messageVC.tabBarItem.title = @"消息";
    }
    
    return _messageVC;
}

- (PYMineViewController *)mineVC
{
    if (!_mineVC) {
        
        _mineVC = [[PYMineViewController alloc] init];
        
        _mineVC.tabBarItem.image = [UIImage imageNamed:@"bottom_wode_moren_icon"];
        
        _mineVC.tabBarItem.selectedImage = [UIImage imageNamed:@"bottom_wode_xzh_icon"];
    
        _mineVC.tabBarItem.title = @"我的";
    }
    
    return _mineVC;
}

- (void)viewDidLoad {
    
    [super viewDidLoad];

    self.tabBar.translucent = YES;
    
    self.tabBar.barStyle = UIBarStyleDefault;
    
    self.tabBar.itemPositioning = UITabBarItemPositioningFill;
    
    self.delegate = self;
    
    UIBarButtonItem *leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"学兮教育" style:UIBarButtonItemStylePlain target:self action:nil];
    
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
    
    UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"sousuo_icon"] style:UIBarButtonItemStylePlain target:self action:@selector(searchAction)];
    
    self.navigationItem.rightBarButtonItem = rightBarButtonItem;
    
    self.viewControllers = @[self.liveLessonVC, self.communityVC, self.messageVC, self.mineVC];

    self.tabBar.tintColor = [UIColor colorWithRed:0.47 green:0.81 blue:0.93 alpha:1.00];
    
    self.tabBar.unselectedItemTintColor = [UIColor colorWithRed:0.78 green:0.78 blue:0.78 alpha:1.00];
}

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
    self.navigationItem.leftBarButtonItem = nil;
    
    self.navigationItem.rightBarButtonItem = nil;
    
    if (self.selectedIndex == 0) {
        
        UIBarButtonItem *leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"学兮教育" style:UIBarButtonItemStylePlain target:self action:nil];
        
        self.navigationItem.leftBarButtonItem = leftBarButtonItem;
        
        UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"sousuo_icon"] style:UIBarButtonItemStylePlain target:self action:@selector(searchAction)];
        
        self.navigationItem.rightBarButtonItem = rightBarButtonItem;
    
    } else if (tabBarController.selectedIndex == 1) {
        
        
    } else if (tabBarController.selectedIndex == 2) {
        
        PYLoginViewController *loginVC = [PYLoginViewController new];
        
        PYLoginNavigationController *loginNavi = [[PYLoginNavigationController alloc] initWithRootViewController:loginVC];

        [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:loginNavi animated:YES completion:nil];
        
    } else if (tabBarController.selectedIndex == 3) {
        
        
    }
}

- (void)searchAction
{
    PYSearchLiveLessonViewController *searchLiveLessonVC = [[PYSearchLiveLessonViewController alloc] init];
    
    [self.navigationController pushViewController:searchLiveLessonVC animated:YES];
}

@end
